<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
              <el-table-column prop="Pno" label="体检表单号" width="100" align="center"></el-table-column>
                <el-table-column prop="Sno" label="学生学号"></el-table-column>
                <el-table-column prop="Page" label="学生年龄"></el-table-column>
                <el-table-column prop="Pheight" label="学生身高"></el-table-column>
                <el-table-column prop="Pweight" label="学生体重"></el-table-column>
                <el-table-column prop="Pchest" label="学生胸围"></el-table-column>
                <el-table-column prop="Pdate" label="体检日期"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                      <el-button
                          type="primary"
                          icon="el-icon-delete"
                          class="red"
                          @click="handleDelete(scope.row)"
                      >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--添加按钮-->
            <el-button type="primary" class="inline" id="add-physical" @click="addPhysical">添加体检信息</el-button>

            <div class="pagination inline" id="physical-pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="体检表单号">
                    <el-input v-model="partsData.Pno"></el-input>
                </el-form-item>
				<el-form-item label="学生学号">
                    <el-input v-model="partsData.Sno"></el-input>
                </el-form-item>
				<el-form-item label="学生年龄">
                    <el-input v-model="partsData.Page"></el-input>
                </el-form-item>
				<el-form-item label="学生身高">
                <el-input v-model="partsData.Pheight"></el-input>
                </el-form-item>
				<el-form-item label="学生体重">
                <el-input v-model="partsData.Pweight"></el-input>
                </el-form-item>
                <el-form-item label="学生胸围">
                <el-input v-model="partsData.Pchest"></el-input>
                </el-form-item>
                <el-form-item label="体检日期">
                <el-input v-model="partsData.Pdate"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>


        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="体检表单号">
                    <el-input v-model="addPartData.Pno"></el-input>
                </el-form-item>
                <el-form-item label="学生学号">
                    <el-input v-model="addPartData.Sno"></el-input>
                </el-form-item>
                <el-form-item label="学生年龄">
                    <el-input v-model="addPartData.Page"></el-input>
                </el-form-item>
                <el-form-item label="学生身高">
                    <el-input v-model="addPartData.Pheight"></el-input>
                </el-form-item>
                <el-form-item label="学生体重">
                    <el-input v-model="addPartData.Pweight"></el-input>
                </el-form-item>
                <el-form-item label="学生胸围">
                    <el-input v-model="addPartData.Pchest"></el-input>
                </el-form-item>
                <el-form-item label="体检日期">
                    <el-input v-model="addPartData.Pdate"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
      import { physicaldelete,fetchgetphysical } from '../../api/index';
      import { fetchupdatephysical } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            tableData: [],
            tempPartsData:{},
            partsData: {},
            addPartData:{},
            addVisible: false,
            multipleSelection: [],
            delList: [],
            query: {},
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            physicalTitle: "编辑"
        };
    },
    created() {
       this.getData()
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
          fetchgetphysical().then(res =>{console.info(res)
            this.tableData=res.data.value
          })
        },
        handleDelete(row) {
          this.tempPartsData = row
          physicaldelete(this.tempPartsData).then(res=>{console.info(res)
            console.log(this.tempPartsData)
            console.log(res)
            window.location.reload()
          })
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.partsData = row;
            this.physicalTitle="编辑"
            this.editVisible = true
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
           fetchupdatephysical(this.partsData).then(res =>{console.info(res)
             console.log(this.partsData)
             console.log(res)
          })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        //添加体检表
        addPhysical(){
            this.addVisible = true
        },
        //保存添加
        saveAdd(){
            if(this.addPartData.Pno===undefined ||
                this.addPartData.Sno===undefined ||
                this.addPartData.Page===undefined||
                this.addPartData.Pheight===undefined||
                this.addPartData.Pweight===undefined||
                this.addPartData.Pchest===undefined||
                this.addPartData.Pdate===undefined) {
                alert("请把信息填写完整")
            }else {
                //添加体检信息
                physicalAdd(this.addPartData).then(res => {
                    console.log(this.addPartData)
                    console.log(res)
                })
                this.addVisible=false
            }
        }
    }
};
</script>

<style scoped>

.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #f1eeee;
}
.inline{
    display: inline-block;
}

#physical-pagination{
    float:right;
}

#add-physical{
    margin-top:15px;
}
</style>
